package com.data;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.com.R;

public class MyDataActivity extends Activity {
	/** Called when the activity is first created. */
	Helper db;
	EditText Ed_Name, Ed_Number, Ed_Email;
	Button BT_Enter;
	Button BT_GetData;
	Button BT_DeleteData;
	ListView list;
	User user = new User();
	Handler handler;
	Runnable ut;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		init();
		BT_Enter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				handler.post(ut);
				
				ut =new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						dataentry();
					}
				};
				
						
				

			}

			
		});
		BT_GetData.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				getall();
			}
		});
		BT_DeleteData.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				//

			}
		});

	}

	public void init() {
		Ed_Name = (EditText) findViewById(R.id.editText1);
		Ed_Number = (EditText) findViewById(R.id.editText2);
		Ed_Email = (EditText) findViewById(R.id.editText3);
		BT_Enter = (Button) findViewById(R.id.button1);
		BT_GetData = (Button) findViewById(R.id.button2);
		BT_DeleteData = (Button) findViewById(R.id.button3);

		db = new Helper(this);

	}

	public void dataentry() {

		String name = Ed_Name.getText().toString();

		String number = Ed_Number.getText().toString();
		String email = Ed_Email.getText().toString();
		if (name.length() == 0 || number.length() == 0 || email.length() == 0) {
			Toast.makeText(getBaseContext(), "Enter Valid Input",
					Toast.LENGTH_LONG).show();
		} else {
			db.datainit(name, number, email);
			Ed_Name.setText("");
			Ed_Number.setText("");
			Ed_Email.setText("");
		}
	}

	public void getall() {

		Cursor cursor = db.all();
		ArrayList<User> u = new ArrayList<User>();

		cursor.moveToFirst();
		while (cursor.moveToNext()) 
		{

			User user=new User();
			user.setName(cursor.getString(1));
			System.out.println("==>NAME" + (cursor.getString(1).toString()));
			user.setNumber(cursor.getString(2));
			System.out.println("==>NUMBER" + (cursor.getString(2).toString()));
			user.setEmail(cursor.getString(3));
			System.out.println("==>EMAIL" + (cursor.getString(3).toString()));

			u.add(user);

		}
		display(u);

		System.out.println("display2");
		

	}

	public void display(ArrayList<User> u1) {

		myadapter adapter = new myadapter(getBaseContext(), R.layout.raw, u1);

		ListView list = (ListView) findViewById(R.id.mylist);

		list.setAdapter(adapter);
		// u1.clear();

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		db.close();
		finish();
	}

}